package com.elitetyc.customview.custom;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.elitetyc.customview.R;
import com.elitetyc.customview.model.TimeRulerBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by elitetyc on 2019/6/22.
 * Describe:
 */
public class TimeRulerView extends View {


    private List<TimeRulerBean> cantTimes;
    private TimeRulerBean preCheckTime;

    private int perScaleWidth;

    private int rectAngle;
    private int marginLeft;

    private Paint mPaint;

    private int indX=100;

    private int classHour = 1;

    private Drawable redLeftB,redCenterB,redRightB,blueLeftB,blueCenterB,blueRightB,yellowLeftB,yellowCenterB,yellowRightB;

    public TimeRulerView(Context context) {
        this(context,null);
    }

    public TimeRulerView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TimeRulerView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        initData();

    }

    private void init(){
        redLeftB = getResources().getDrawable(R.drawable.shape_time_ruler_left_red);
        redLeftB= getResources().getDrawable(R.drawable.shape_time_ruler_left_red);
        redCenterB= getResources().getDrawable(R.drawable.shape_time_ruler_center_red);
        redRightB= getResources().getDrawable(R.drawable.shape_time_ruler_right_red);


        blueLeftB= getResources().getDrawable(R.drawable.shape_time_ruler_left_blue);
        blueCenterB= getResources().getDrawable(R.drawable.shape_time_ruler_center_blue);
        blueRightB= getResources().getDrawable(R.drawable.shape_time_ruler_right_blue);

        yellowLeftB= getResources().getDrawable(R.drawable.shape_time_ruler_left_yellow);
        yellowCenterB= getResources().getDrawable(R.drawable.shape_time_ruler_center_yellow);
        yellowRightB= getResources().getDrawable(R.drawable.shape_time_ruler_right_yellow);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        perScaleWidth = ScreenUtils.getScreenWidth()/15;
        int widthSize = perScaleWidth*48;
        rectAngle = ConvertUtils.dp2px(10);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
        marginLeft = layoutParams.getMarginStart();
        setMeasuredDimension(widthSize,MeasureSpec.getSize(heightMeasureSpec));
    }

    private void initData() {
        cantTimes = new ArrayList<>();
        cantTimes.add(new TimeRulerBean(0,4));
        cantTimes.add(new TimeRulerBean(7,8));
        cantTimes.add(new TimeRulerBean(22,24));
        preCheckTime = new TimeRulerBean(18,20);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        setBackgroundResource(R.drawable.shape_time_ruler_bg);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //        画刻度
        mPaint.setColor(0xff8C8C8C);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(ConvertUtils.dp2px(1));
        mPaint.setTextSize(ConvertUtils.dp2px(12));
        for (int i=1;i<48;i++){
            if (i%2==0){
//                画长刻度线
                canvas.drawLine(i*perScaleWidth,0,i*perScaleWidth,ConvertUtils.dp2px(20),mPaint);
                String s = i/2+":00";
                float width = mPaint.measureText(s);
                canvas.drawText(s,-width/2+i*perScaleWidth,ConvertUtils.dp2px(40),mPaint);
            }else {
//                画短刻度线
                canvas.drawLine(i*perScaleWidth,0,i*perScaleWidth,ConvertUtils.dp2px(10),mPaint);

            }
        }


//        画不同时间的背景颜色
        mPaint.setColor(0x80EEB4B4);
        mPaint.setStyle(Paint.Style.FILL);
        for (TimeRulerBean timeRulerBean:cantTimes){
            int width1 = (int)(timeRulerBean.startTime / 0.5 * perScaleWidth);
            int width2 = (int)(timeRulerBean.endTime / 0.5 * perScaleWidth);
            RectF rectFBg1;
            if (timeRulerBean.startTime==0){
                redLeftB.setBounds(new Rect(width1+ConvertUtils.dp2px(1),ConvertUtils.dp2px(1),
                        width2,getMeasuredHeight()-ConvertUtils.dp2px(1)));
                redLeftB.draw(canvas);
            }else if (timeRulerBean.endTime==24){
                redRightB.setBounds(new Rect(width1,ConvertUtils.dp2px(1),
                        width2-ConvertUtils.dp2px(1),getMeasuredHeight()-ConvertUtils.dp2px(1)));
                redRightB.draw(canvas);
            }else {
                redCenterB.setBounds(new Rect(width1,ConvertUtils.dp2px(1),
                        width2,getMeasuredHeight()-ConvertUtils.dp2px(1)));
                redCenterB.draw(canvas);
            }

        }
//        画未调时间背景
        mPaint.setColor(0x80EEE9BF);
        mPaint.setStyle(Paint.Style.FILL);
        int width1 = (int)(preCheckTime.startTime / 0.5 * perScaleWidth);
        int width2 = (int)(preCheckTime.endTime / 0.5 * perScaleWidth);
        if (preCheckTime.startTime==0){
            yellowLeftB.setBounds(new Rect(width1+ConvertUtils.dp2px(1),ConvertUtils.dp2px(1),
                    width2,getMeasuredHeight()-ConvertUtils.dp2px(1)));
            yellowLeftB.draw(canvas);
        }else if (preCheckTime.endTime==24){
            yellowRightB.setBounds(new Rect(width1,ConvertUtils.dp2px(1),
                    width2-ConvertUtils.dp2px(1),getMeasuredHeight()-ConvertUtils.dp2px(1)));
            yellowRightB.draw(canvas);
        }else {
            yellowCenterB.setBounds(new Rect(width1,ConvertUtils.dp2px(1),
                    width2,getMeasuredHeight()-ConvertUtils.dp2px(1)));
            yellowCenterB.draw(canvas);
        }


//        画调课后的时间背景
        blueCenterB.setBounds(new Rect(indX,ConvertUtils.dp2px(1),indX+classHour*2*perScaleWidth,getMeasuredHeight()-ConvertUtils.dp2px(1)));
        blueCenterB.draw(canvas);


//        画指示器
        mPaint.setColor(0xff4169E1);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(ConvertUtils.dp2px(3));
        Path path = new Path();
        path.moveTo(indX,0);
        path.lineTo(indX,getMeasuredHeight()-ConvertUtils.dp2px(15));
        canvas.drawPath(path,mPaint);

        mPaint.setStyle(Paint.Style.FILL);
        Path path2 = new Path();
        path2.moveTo(indX,getMeasuredHeight()-ConvertUtils.dp2px(18));
        path2.lineTo(indX-ConvertUtils.dp2px(10),getMeasuredHeight()-ConvertUtils.dp2px(5));
        path2.lineTo(indX+ConvertUtils.dp2px(10),getMeasuredHeight()-ConvertUtils.dp2px(5));
        path2.close();
        canvas.drawPath(path2,mPaint);


    }

    private int mLastX;
    private boolean moveInd = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastX = (int) event.getX();
                if (mLastX<=indX+classHour*2*perScaleWidth&&mLastX>=indX-ConvertUtils.dp2px(10)){
                    moveInd = true;
                }else {
                    moveInd = false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int x1 = (int) event.getX();
                if (moveInd){
//                    就说明在移动指示器，设置指示器的值
                    indX = x1;
                    for (int i=1;i<48;i++){
                        if (indX>=i*perScaleWidth&&indX<=(i+1)*perScaleWidth){
                            if (i*perScaleWidth+classHour*2*perScaleWidth>47*perScaleWidth){
                                setIndX((47-classHour*2)*perScaleWidth);
                            }else {
                                setIndX(i*perScaleWidth);
                            }
                        }
                    }

                }else {
//                    移动尺子
                    this.setTranslationX(getTranslationX()+x1-mLastX);
                    if (getTranslationX()>0){
                        setTranslationX(0);
                    }else if (getTranslationX()<(-48*perScaleWidth)+15*perScaleWidth-marginLeft*2){
                        setTranslationX((-48*perScaleWidth)+15*perScaleWidth-marginLeft*2);
                    }
                }


                break;
            case MotionEvent.ACTION_UP:
                int x2 = (int) event.getX();
                if (mLastX==x2){
//                    说明是点击了一下，就将指示器移动到这个位置
                    for (int i=1;i<48;i++){
                        if (x2>=i*perScaleWidth&&x2<=(i+1)*perScaleWidth){
                            if (i*perScaleWidth+classHour*2*perScaleWidth>47*perScaleWidth){
                                setIndX((47-classHour*2)*perScaleWidth);
                            }else {
                                setIndX(i*perScaleWidth);
                            }
                        }
                    }
                }
                mLastX = x2;
                break;
        }
        return true;
    }

    public interface onTimeChangeListener{
        void onTimeChangeed(double startTime,double endTime,boolean hasError);
    }

    private onTimeChangeListener onTimeChangeListener;

    public void setOnTimeChangeListener(TimeRulerView.onTimeChangeListener onTimeChangeListener) {
        this.onTimeChangeListener = onTimeChangeListener;
    }

    public void setIndX(int indX) {
        this.indX = indX;
        double startTime = (double)(indX/perScaleWidth)/2;
        double endTime = startTime+classHour;
        boolean hasError = false;
        for(TimeRulerBean timeRulerBean:cantTimes){
            if ((startTime>timeRulerBean.startTime&&startTime<timeRulerBean.endTime)||
                    (endTime>timeRulerBean.startTime&&endTime<timeRulerBean.endTime)){
                hasError = true;
                break;
            }
        }
        if (onTimeChangeListener!=null){
            onTimeChangeListener.onTimeChangeed(startTime,endTime,hasError);
        }
        invalidate();
    }
}
